package com.cjb.systemconfig.shiro;

import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import sun.security.krb5.Realm;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
       ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean ();
       bean.setLoginUrl("/login");
       bean.setSuccessUrl("/index");
       bean.setUnauthorizedUrl("/error");
       bean.setSecurityManager(securityManager);
       Map<String,String> filters=new HashMap<String,String>();
        filters.put("/login","anon");
        filters.put("/login/dologin","anon");
        filters.put("/js/**","anon");
        filters.put("/style/**","anon");
        filters.put("/bootstrap/**","anon");
        filters.put("/ztree/**","anon");
        filters.put("/assets-3rd/**","anon");
        filters.put("/**","authc");
       bean.setFilterChainDefinitionMap(filters);
        return bean;
    }

    @Bean("securityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("platformReaml")AuthorizingRealm realm){
        DefaultWebSecurityManager bean=new DefaultWebSecurityManager();
        bean.setRealm(realm);
        return bean;

    }

    /**
     *  开启shiro aop注解支持.
     *  使用代理方式;所以需要开启代码支持;
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }






}
